import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import {tranResDataToTreeData} from "../common/utils";
import {getCommentForUnchecked} from "../network/viewsRequest/comment";
import {getAllBackUsableNotice} from "../network/viewsRequest/notice";

import {PORTAL_SERVER_URL} from "../common/const";

export default new Vuex.Store({
  state: {
    showAside: true, // 是否显示侧边栏
    uncheckCommentDataForNavMenu: { // header区域中显示的没有审核的记录信息
      list: [], // 具体未审核的数据
      total: 0, // 未审核的数量
    },
    showNoticeData: [], // 公告子组件显示的公告信息
    token: '', // 登录令牌
    allowCheckCommentUser: [], // 所有拥有审核评论的用户名称的集合
    currentUser: { // 当前登录的用户信息
      username: '', // 用户名
      permission: {
        menuList: [],
        buttonList: [],
      },

    }
  },
  mutations: {
    /**
     * 给currentUser赋值
     * @param profile 服务器端返回的安全数据
     */
    assignCurrentUser(state, profile) {
      let menus = tranResDataToTreeData(profile.permissionMap.menus);
      state.allowCheckCommentUser = profile.allowCheckCommentUser;
      state.currentUser.username = profile.username;
      state.currentUser.permission.menuList = menus;
      state.currentUser.permission.buttonList = profile.permissionMap.buttons;
    },
    clearCurrentUser(state) {
      state.token = '';
      state.allowCheckCommentUser = [];
      state.currentUser = {
        username: '',
        permission: {
          menuList: [],
          buttonList: []
        }
      }
    },
    /**
     * 根据浏览器宽度决定是否要进行侧边栏的隐藏
     * @param state
     * @param width 浏览器宽度
     */
    changeShowAside(state, width) {
      if (!width) { // 不传递width参数时，让showAside取反
        this.state.showAside = !this.state.showAside;
      } else { // 传递width参数时，如果宽度大于768赋值true，反之false
        this.state.showAside = width > 768;
      }
      // console.log("此次修改后，值为" + this.state.showAside);
    },
    /**
     * 获取未审核的评论信息
     */
    getCommentForUnchecked(state) {
      getCommentForUnchecked().then(res => {
        let uncheckedCommentData;
        if (res.data.items != null) {
          uncheckedCommentData = res.data.items.map(item => {
            // 1.处理authorPhoto字段
            item.authorPhoto = PORTAL_SERVER_URL + '/static/images/avatar/' + item.authorPhoto;
            // 2.处理article字段
            if (item.article.length > 15) {
              item.article = item.article.substring(0, 15) + '...';
            }
            return item;
          });
        } else {
          uncheckedCommentData = [];
        }

        state.uncheckCommentDataForNavMenu.list = uncheckedCommentData;
        state.uncheckCommentDataForNavMenu.total = res.data.total;
      })
    },
    /**
     * 获取后端所有启用的公告
     */
    getAllBackUsableNotice(state) {
      getAllBackUsableNotice().then(res => {
        if (res.data != null && res.data.length !== 0) {
          state.showNoticeData = res.data;
        } else {
          state.showNoticeData = [{content: '暂时没有公告'}];
        }
      });
    },
    /**
     * 设置token
     * @param state
     * @param token
     */
    assignToken(state, token) {
      state.token = token;
    },
  },
  actions: {
  },
  modules: {
  }
})
